export default {
  state: {
    page: {},
    articles: [],
    newArticles: [],
    modules: [],
    currentModule: null,
    name: null
  },

  mutations: {
    SET_ARTICLES: (state, { articles, page }) => {
      state.page = page
      state.articles = articles
    },

    SET_NEW_ARTICLES: (state, { newArticles }) => {
      state.newArticles = newArticles
    },

    SET_MODULES: (state, { modules }) => {
      state.modules = modules
    }
  },

  actions: {
    FETCH_ARTICLE_DATA: ({ dispatch, commit }, { params }) => {
      return Promise.all([dispatch('FETCH_ARTICLES', { params }), dispatch('FETCH_MODULES')])
    },

    FETCH_NEW_ARTICLE: ({ dispatch, commit }, { params }) => {
      return dispatch('FETCH_ENDPOINT_DATA', {
          endpoint: 'new-articles',
          params: params
        })
        .then(({ newArticles, page }) => {
          commit('SET_NEW_ARTICLES', { newArticles, page })
        })
    },

    FETCH_ARTICLES: ({ dispatch, commit }, { params }) => {
      return dispatch('FETCH_ENDPOINT_DATA', {
          endpoint: 'articles',
          params: params
        })
        .then(({ articles, page }) => {
          commit('SET_ARTICLES', { articles, page })
        })
    },

    FETCH_MODULES: ({ dispatch, commit }) => {
      return dispatch('FETCH_ENDPOINT_DATA', { endpoint: 'modules'})
        .then(({ modules }) => { commit('SET_MODULES', { modules }) })
    }
  }
}